package com.nf.health.app.inquiry.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.nf.health.app.R;
import com.nf.health.app.models.ChatMessageVo;
import com.nf.health.app.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 恩福 on 2016/9/28.
 */

public class InquiryChatListAdapter extends RecyclerView.Adapter<InquiryChatListAdapter.MyViewHolder>{

    private Context context;
    private List<ChatMessageVo> messages;
    private String type;
    private static final String MSG_SEND_TXT = "1";
    private static final String MSG_RECEIVE_TXT = "2";
    private static final String MSG_SEND_IMAGE = "3";
    private static final String MSG_RECEIVE_IMAGE = "4";
    private RecyclerView recyclerView;

    public InquiryChatListAdapter(Context context, List<ChatMessageVo> messages, RecyclerView recyclerView) {
        this.context = context;
        this.messages = messages;
        this.recyclerView = recyclerView;
        initData();
    }

    private void initData() {
        if(messages == null){
            messages = new ArrayList<>();
        }
        recyclerView.smoothScrollToPosition(messages.size());
    }

    @Override
    public InquiryChatListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.inquiry_chat_item,null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final InquiryChatListAdapter.MyViewHolder holder, int position) {
        if(messages == null){
            Toast.makeText(context, "请检查网络", Toast.LENGTH_SHORT).show();
            return;
        }
        final ChatMessageVo message = messages.get(position);
        type = message.getType();
        switch (type){
            case MSG_SEND_TXT:
                // 设置内容,加环信表情20170313
                Spannable span1 = EaseSmileUtils.getSmiledText(context, message.getMessage());
                holder.tvSendChatcontent.setText(span1, TextView.BufferType.SPANNABLE);
//                holder.tvSendChatcontent.setText(message.getMessage());
                message.setIsRecVisable(true);
                break;
            case MSG_RECEIVE_TXT:
                Spannable span2 = EaseSmileUtils.getSmiledText(context, message.getMessage());
                holder.tvRecChatcontent.setText(span2, TextView.BufferType.SPANNABLE);
//                holder.tvRecChatcontent.setText(message.getMessage());
                message.setIsRecVisable(false);
                break;
            case MSG_SEND_IMAGE:
                Glide.with(context).load(message.getMessage()).centerCrop().into(holder.sendImage);
                message.setIsRecVisable(true);
                break;
            case MSG_RECEIVE_IMAGE:
                Glide.with(context).load(message.getMessage()).centerCrop().into(holder.recImage);
                message.setIsRecVisable(false);
                break;
        }
        if(message.getIsRecVisable()){
            holder.recBubble.setVisibility(View.GONE);
            holder.sendBubble.setVisibility(View.VISIBLE);
            if(MSG_SEND_IMAGE.equals(type)){
                holder.recImage.setVisibility(View.GONE);
                holder.sendImage.setVisibility(View.VISIBLE);
                holder.tvSendChatcontent.setVisibility(View.GONE);
            }else if(MSG_SEND_TXT.equals(type)){
                holder.recImage.setVisibility(View.GONE);
                holder.sendImage.setVisibility(View.GONE);
                holder.tvSendChatcontent.setVisibility(View.VISIBLE);
            }
        }else{
            holder.recBubble.setVisibility(View.VISIBLE);
            holder.sendBubble.setVisibility(View.GONE);
            if(MSG_RECEIVE_IMAGE.equals(type)){
                holder.recImage.setVisibility(View.VISIBLE);
                holder.sendImage.setVisibility(View.GONE);
                holder.tvRecChatcontent.setVisibility(View.GONE);
            }else if(MSG_RECEIVE_TXT.equals(type)){
                holder.recImage.setVisibility(View.GONE);
                holder.sendImage.setVisibility(View.GONE);
                holder.tvRecChatcontent.setVisibility(View.VISIBLE);
            }
        }

        //设置时间
        if (position == 0) {
            holder.timestamp.setText(DateUtil.date2String(getItemCreatetime(position), "MM月dd日 HH时mm分"));
            holder.timestamp.setVisibility(View.VISIBLE);
        } else {
            //两条消息时间离得如果稍长，显示时间
            if (DateUtil.isCloseEnough(getItemCreatetime(position),getItemCreatetime(position - 1))) {
                holder.timestamp.setVisibility(View.GONE);
            } else {
                holder.timestamp.setText(DateUtil.date2String(getItemCreatetime(position), "MM月dd日 HH时mm分"));
                holder.timestamp.setVisibility(View.VISIBLE);
            }
        }

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.sendImage.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemImageClick(holder.itemView, pos, message.getMessage());
                }
            });

            holder.recImage.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemImageClick(holder.itemView, pos, message.getMessage());
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout recBubble;
        RelativeLayout sendBubble;
        TextView tvSendChatcontent;
        TextView tvRecChatcontent;
        TextView timestamp;
        ImageView sendImage;
        ImageView recImage;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvSendChatcontent = (TextView) itemView.findViewById(R.id.tv_send_chatcontent);
            tvRecChatcontent = (TextView) itemView.findViewById(R.id.tv_rec_chatcontent);
            timestamp = (TextView) itemView.findViewById(R.id.timestamp);
            recBubble = (RelativeLayout) itemView.findViewById(R.id.rec_bubble);
            sendBubble = (RelativeLayout) itemView.findViewById(R.id.send_bubble);
            recImage = (ImageView) itemView.findViewById(R.id.rec_image);
            sendImage = (ImageView) itemView.findViewById(R.id.send_image);
        }
    }

    //向集合中添加Item
    public void addItem(ChatMessageVo msg){
        messages.add(msg);
        notifyDataSetChanged();
        recyclerView.smoothScrollToPosition(messages.size() - 1);
    }

    public void addAllMessage(List<ChatMessageVo> messages){
        this.messages.clear();
        this.messages.addAll(messages);

        notifyDataSetChanged();
        recyclerView.smoothScrollToPosition(messages.size() - 1);
    }

    //监听事件
    public interface OnItemClickLitener
    {
        void onItemImageClick(View view, int position, String imageUrl);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public long getItemCreatetime(int position) {
        return messages.get(position).getCreatetime();
    }

}
